/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

using System;

using TypeElement = argot.TypeElement;
using TypeException = argot.TypeException;
using TypeInputStream = argot.TypeInputStream;
using TypeOutputStream = argot.TypeOutputStream;
using TypeReader = argot.TypeReader;
using TypeReaderAuto = argot.TypeReaderAuto;
using TypeWriter = argot.TypeWriter;

namespace argot.meta
{
	
	/// <summary> 
	/// An Identified object uses a u16 to write out the type of the object
	/// to be written.  This allows for things like an array of known objects
	/// to be written.
	/// </summary>
	public class MetaIdentified
	:MetaBase, TypeReader, TypeWriter, MetaExpression
	{

		public static System.String TYPENAME = "meta.identified";
		
		private System.String _description;
		
		public MetaIdentified(System.String description)
		{
			_description = description;
		}

		override public System.String TypeName
		{
			get
			{
				return TYPENAME;
			}
			
		}		

		public virtual System.Object Read(TypeInputStream inStream, TypeElement element)
		{
			TypeReader reader = new TypeReaderAuto(this.GetType());
			return reader.Read(inStream, element);
		}
		
		public virtual void Write(TypeOutputStream outStream, System.Object o, TypeElement element)
		{
			MetaIdentified ti = (MetaIdentified) o;
			
			outStream.WriteObject("meta.name", ti._description);
		}
		
		/* 
		* This is identified function that Identified performs. It reads
		* a U16 and looks up that object and will check if the parameters
		* are in the exclusive set of if any object can be loaded.
		*/
		public virtual System.Object DoRead(TypeInputStream inStream)
		{
			System.Int32 id = (System.Int32) inStream.ReadObject("u16");
			return inStream.ReadObject(id);
		}
		
		public virtual void DoWrite(TypeOutputStream outStream, System.Object o)
		{
			throw new TypeException("not implemented");
		}
	}
}